<?php
 
class Brands extends Admin_Controller { 
    
    function __construct()
    {       
        parent::__construct();
        
        $this->auth->check_access('Admin', true);
        $this->lang->load('brand');
        $this->load->model('Brand_model');
    }
    
    function index()
    {
        //we're going to use flash data and redirect() after form submissions to stop people from refreshing and duplicating submissions
        //$this->session->set_flashdata('message', 'this is our message');
        
        $data['page_title'] = lang('brands');
        $data['brands'] = $this->Brand_model->get_brands_tiered(true);
        
        $this->view($this->config->item('admin_folder').'/brands', $data);
    }
    
    function organize($id = false)
    {
        $this->load->helper('form');
        $this->load->helper('formatting');
        
        if (!$id)
        {
            $this->session->set_flashdata('error', lang('error_must_select'));
            redirect($this->config->item('admin_folder').'/brands');
        }
        
        $data['brand']       = $this->brand_model->get_brand($id);
        //if the brand does not exist, redirect them to the brand list with an error
        if (!$data['brand'])
        {
            $this->session->set_flashdata('error', lang('error_not_found'));
            redirect($this->config->item('admin_folder').'/brands');
        }
            
        $data['page_title']     = sprintf(lang('organize_brand'), $data['brand']->name);
        
        $data['brand_products']  = $this->brand_model->get_brand_products_admin($id);
        
        $this->view($this->config->item('admin_folder').'/organize_brand', $data);
    }
    
    function process_organization($id)
    {
        $products   = $this->input->post('product');
        $this->brand_model->organize_contents($id, $products);
    }
    
    function form($id = false)
    {
        
        $config['upload_path']      = 'uploads/images/full';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = $this->config->item('size_limit');
        $config['max_width']        = '1024';
        $config['max_height']       = '768';
        $config['encrypt_name']     = true;
        $this->load->library('upload', $config);
        
        
        $this->brand_id  = $id;
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $data['brands']     = $this->Brand_model->get_brands();
        $data['page_title']     = lang('brand_form');
        
        //default values are empty if the customer is new
        $data['id']             = '';
        $data['name']           = '';
        $data['slug']           = '';
        $data['description']    = '';
        $data['sequence']       = '';
        $data['image']          = '';
        $data['seo_title']      = '';
        $data['meta']           = '';
        $data['enabled']        = '';
        $data['error']          = '';
        
        //create the photos array for later use
        $data['photos']     = array();
        
        if ($id)
        {   
            $brand       = $this->Brand_model->get_brand($id);

            //if the brand does not exist, redirect them to the brand list with an error
            if (!$brand)
            {
                $this->session->set_flashdata('error', lang('error_not_found'));
                redirect($this->config->item('admin_folder').'/brands');
            }
            
            //helps us with the slug generation
            $this->brand_name    = $this->input->post('slug', $brand->slug);
            
            //set values to db values
            $data['id']             = $brand->id;
            $data['name']           = $brand->name;
            $data['slug']           = $brand->slug;
            $data['description']    = $brand->description;
            $data['sequence']       = $brand->sequence;            
            $data['seo_title']      = $brand->seo_title;
            $data['meta']           = $brand->meta;
            $data['enabled']        = $brand->enabled;
			$data['image']        	= $brand->image;
            
        }
        
        $this->form_validation->set_rules('name', 'lang:name', 'trim|required|max_length[64]');
        $this->form_validation->set_rules('slug', 'lang:slug', 'trim');
        $this->form_validation->set_rules('description', 'lang:description', 'trim');
        $this->form_validation->set_rules('sequence', 'lang:sequence', 'trim|integer');
        $this->form_validation->set_rules('seo_title', 'lang:seo_title', 'trim');
        $this->form_validation->set_rules('meta', 'lang:meta', 'trim');
        $this->form_validation->set_rules('enabled', 'lang:enabled', 'trim|numeric');
        
        
        // validate the form
        if ($this->form_validation->run() == FALSE)
        {
            $this->view($this->config->item('admin_folder').'/brand_form', $data);
        }
        else
        {            
            $this->load->helper('text');
            
            //first check the slug field
            $slug = $this->input->post('slug');
            
            //if it's empty assign the name field
            if(empty($slug) || $slug=='')
            {
                $slug = $this->input->post('name');
            }
            
            $slug   = url_title(convert_accented_characters($slug), 'dash', TRUE);
            
            //validate the slug
            $this->load->model('Routes_model');
            if($id)
            {
                $slug   = $this->Routes_model->validate_slug($slug, $brand->route_id);
                $route_id   = $brand->route_id;
            }
            else
            {
                $slug   = $this->Routes_model->validate_slug($slug);
                
                $route['slug']  = $slug;    
                $route_id   = $this->Routes_model->save($route);
            }
			
			$config['upload_path']      = 'uploads/images/full/';
			$config['allowed_types']    = 'gif|jpg|png';
			$config['max_size']         = 1024;
			$config['max_width']        = '1024';
			$config['max_height']       = '768';
			$config['encrypt_name']     = true;
			$this->load->library('upload', $config);			
            $uploaded   = $this->upload->do_upload('image');
			if(!$uploaded){
               $data['error']  = $this->upload->display_errors();
			}
			else{
				$image          = $this->upload->data();
				$save['image']  = $image['file_name'];
				
				$this->load->library('image_lib');
	
				//cropped thumbnail
				$config['image_library'] = 'gd2';
				$config['source_image'] = 'uploads/images/full/'.$save['image'];
				$config['new_image']    = 'uploads/images/thumbnails/'.$save['image'];
				$config['maintain_ratio'] = TRUE;
				$config['width'] = 150;
				$config['height'] = 150;
				$this->image_lib->initialize($config);  
				$this->image_lib->resize(); 
				$this->image_lib->clear();
			}
			
            $save['id']             = $id;
            $save['name']           = $this->input->post('name');
            $save['description']    = $this->input->post('description');
            $save['sequence']       = intval($this->input->post('sequence'));
            $save['seo_title']      = $this->input->post('seo_title');
            $save['meta']           = $this->input->post('meta');
            $save['enabled']        = $this->input->post('enabled');
            $save['route_id']       = intval($route_id);
            $save['slug']           = $slug;
            
            $brand_id    = $this->Brand_model->save($save);
            
            //save the route
            $route['id']    = $route_id;
            $route['slug']  = $slug;
            $route['route'] = 'cart/brand/'.$brand_id.'';
            
            $this->Routes_model->save($route);
            
            $this->session->set_flashdata('message', lang('message_brand_saved'));
            
            //go back to the brand list
            redirect($this->config->item('admin_folder').'/brands');
        }
    }

    function delete($id)
    {
        
        $brand   = $this->brand_model->get_brand($id);
        //if the brand does not exist, redirect them to the customer list with an error
        if ($brand)
        {
            $this->load->model('Routes_model');
            
            $this->Routes_model->delete($brand->route_id);
            $this->brand_model->delete($id);
            
            $this->session->set_flashdata('message', lang('message_delete_brand'));
            redirect($this->config->item('admin_folder').'/brands');
        }
        else
        {
            $this->session->set_flashdata('error', lang('error_not_found'));
        }
    }
}